package main

import (
	"context"
	authpb "coolcar/proto/auth_out/v1"
	"coolcar/srvs/auth_srv/dao"
	"coolcar/srvs/auth_srv/handler"
	"log"
	"net"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"

	"go.uber.org/zap"

	"google.golang.org/grpc"
)

func main() {
	logger, err := newZapLogger()
	if err != nil {
		log.Fatalf("can not create logger: %v", err)
	}

	lis, _ := net.Listen("tcp", ":50051")
	if err != nil {
		logger.Fatal("can listen: %v", zap.Error(err))
	}

	mongoClient, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb://localhost:27017/coolcar?readPreference=primary&appname=mongodb-vscode%200.7.0&directConnection=true&ssl=false"))
	if err != nil {
		logger.Fatal("can not connect mongodb: %v", zap.Error(err))
	}
	m := dao.NewMongo(mongoClient.Database("coolcar"))

	s := grpc.NewServer()
	authpb.RegisterAuthServiceServer(s, &handler.Service{
		Mongo:  m,
		Logger: logger,
	})

	log.Fatal(s.Serve(lis))
}

func newZapLogger() (*zap.Logger, error) {
	cfg := zap.NewDevelopmentConfig()
	cfg.EncoderConfig.TimeKey = ""
	return cfg.Build()
}
